﻿using System;
using System.Collections;
using System.Collections.Generic;
using Business.Domain.Application;
using Business.Domain.Wms;
using Business.Common.Exception;
using System.ServiceModel;

namespace Wms.Common
{
    public class GlobalState
    {
        public static string ApplicationCode = "WMS";

        public static Application CurrentApplication;

        /// <summary>
        /// 登录用户
        /// </summary>
        public static User LoginUser;

        /// <summary>
        /// 登录用户所在公司
        /// </summary>
        public static Company LoginUserCompany;

        /// <summary>
        /// 登录用户权限列表
        /// </summary>
        public static List<string> LoginUserAuthority;

        /// <summary>
        /// 多语言管理器
        /// </summary>
        //public static LanguageHelper LanguageHelper;

        /// <summary>
        /// 客户端打印机序号
        /// </summary>
        public static int PrintIndex;

        /// <summary>
        /// 缺省货主编号
        /// </summary>
        public static int DefaultOwnerId;

        /// <summary>
        /// 是否仓库操作员
        /// </summary>
        private static bool _isWarehouseUser;
        public static bool IsWarehouseUser
        {
            get
            {
               
                return _isWarehouseUser;
            }
            set { _isWarehouseUser = value; }
        }

        /// <summary>
        /// 登录用户所在仓库编号
        /// </summary>
        private static int _loginWarehouseId;
        public static int LoginWarehouseId
        {
            set 
            {
                _loginWarehouseId = value;
            }
            get
            {
                return _loginWarehouseId;
            }
        }

        /// <summary>
        /// 登录用户所在仓库名称
        /// </summary>
        private static string _loginWarehouseName;
        public static string LoginWarehouseName
        {
            set
            {
                _loginWarehouseName = value;
            }
            get
            {
                return _loginWarehouseName;
            }
        }

        static GlobalState()
        {
            //string languageCode = System.Configuration.ConfigurationManager.AppSettings["LanguageCode"];
            //if (languageCode == "")
            //    LanguageHelper = new MultiLanguageHelper("zh-CHS");
            //else
            //    LanguageHelper = new MultiLanguageHelper(languageCode);

            string printConfig = System.Configuration.ConfigurationManager.AppSettings["PrintIndex"];
            try
            {
                PrintIndex = Int32.Parse(printConfig);
            }
            catch (Exception)
            {
                PrintIndex = 0;
            }


            try
            {
                CurrentApplication = ServiceHelper.ApplicationService.GetApplicationByCode(ApplicationCode);
            }
            catch (FaultException<ServiceError> sex)
            {
                if (sex.Detail != null)
                    FormHelper.ShowWarningDialog(sex.Detail.ErrorMessage);
            }

            const string parameterName = "DEFAULT_OWNER_COMPANYCODE";
            Parameter parameter = ServiceHelper.ApplicationService.GetParameterByCode(ApplicationCode, parameterName);
            if (parameter != null)
            {
                string companyCode = parameter.ParameterValue;
                //DefaultOwnerId = BusinessKit.GetCompanyId(companyCode);
            }

            //LoginUser = new User();
            //LoginUser.UserId = 10000;
            //LoginUser.UserCode = "Test";
            //LoginUser.UserName = "Test";
        }
    }
}
